package com.example.wangzh.developkit.control;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.base.BaseActivityNew;
import com.example.wangzh.developkit.constant.LogTag;
import com.example.wangzh.developkit.databinding.ActivityLogTestBinding;
import com.wzh.advancelog.log.WzhLog;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * @author: zhwang37
 * @date: 2021/8/30 11:37
 * @email:zhwang37@iflytek.com
 * @describe:
 */
public class LogTestActivity extends BaseActivityNew<ActivityLogTestBinding> {
    Disposable disposable;
    String str;
    ObjectAnimator objectAnimator;

    @Override
    protected void initView() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 5000; i++) {
            stringBuilder.append(i);
        }
        str = stringBuilder.toString();
        objectAnimator = ObjectAnimator.ofFloat(mBinding.btn, "rotation", 0f, 360f);
        objectAnimator.setDuration(2000).setRepeatCount(ValueAnimator.INFINITE);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_log_test;
    }

    @Override
    protected void initEvent() {
        mBinding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startLogTest();
                startAnim();
            }
        });
        mBinding.stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopLogTest();
                stopAnim();
            }
        });
        mBinding.query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }


    private void stopLogTest() {
        disposable.dispose();
    }

    private void stopAnim() {
        objectAnimator.pause();
    }

    private void startLogTest() {
        Observable.interval(50, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposable = d;
            }

            @Override
            public void onNext(@NonNull Long aLong) {
                WzhLog.i(LogTag.TEST, aLong + ":" + str);
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void startAnim() {
        objectAnimator.start();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) {
            disposable.dispose();
        }
    }
}
